<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.wechat.tools.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('business.wechat.tools.title')" prop="title">
            <el-input :placeholder="i18n('common.please_input') + i18n('business.wechat.tools.title')"
              v-model="dataForm.title" show-word-limit maxlength="20"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('business.wechat.tools.picture_enable')" prop="picture_enable">
            <el-upload
              class="avatar-uploader"
              :action="$http.adornUrl('/file/picture')"
              :show-file-list="false"
              :headers="upload_headers"
              :on-success="handlePictureSuccess"
              :before-upload="beforePictureUpload">
              <img v-if="dataForm.picture_enable" :src="dataForm.picture_enable" class="avatar-upload"/>
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>

          <el-form-item :label="i18n('business.wechat.tools.picture_disable')" prop="picture_disable">
            <el-upload
              class="avatar-uploader"
              :action="$http.adornUrl('/file/picture')"
              :show-file-list="false"
              :headers="upload_headers"
              :on-success="handlePictureDisableSuccess"
              :before-upload="beforePictureUpload">
              <img v-if="dataForm.picture_disable" :src="dataForm.picture_disable" class="avatar-upload"/>
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>

          <el-form-item :label="i18n('business.wechat.tools.url')" prop="url">
            <el-input :placeholder="i18n('common.please_input') + i18n('business.wechat.tools.url')"
              v-model="dataForm.url" show-word-limit maxlength="100"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('business.wechat.tools.language')" prop="language">
            <el-select
              v-model="dataForm.language"
              :placeholder="i18n('common.please_select') + i18n('business.wechat.tools.language')" clearable>
              <el-option v-for="(v, k) in languageList" :label="v.text" :key="k" :value="v.value"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('common.sort')" prop="sort">
            <el-input-number :placeholder="i18n('common.please_input') + i18n('common.sort')" v-model="dataForm.sort"></el-input-number>
          </el-form-item>

          <el-form-item>
            <el-button v-if="isAuth('module:business:wechat:tools:handle')" type="primary" @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-wechat-tools-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/wechat/tools')

  const languageList = ref([])
  const status = ref(false)

  const dataForm = reactive({
    id: 0,
    title: '',
    picture_enable: '',
    picture_disable: '',
    url: '',
    language: 'zh',
    sort: 0,
  })

  const dataRule = reactive({
    title: [{
      required: true,
      message: i18n('business.wechat.tools.rules.title.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.title = data.data.title
            dataForm.picture_enable = data.data.picture_enable
            dataForm.picture_disable = data.data.picture_disable
            dataForm.url = data.data.url
            dataForm.language = data.data.language.value
            dataForm.sort = data.data.sort
          }
        })
      }
    })
  }

  const loadLanguageList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'language',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        languageList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2024-10-21
   *
   * 启用图片上传成功方法
   *
   * @param {[type]} res 上传结果
   * @param {[type]} file 上传文件
   * @return {[type]}
   */
  const handlePictureSuccess = (res, file) => {
    dataForm.picture_enable = res.data
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2024-10-21
   *
   * 禁用图片上传成功方法
   *
   * @param {[type]} res 上传结果
   * @param {[type]} file 上传文件
   * @return {[type]}
   */
  const handlePictureDisableSuccess = (res, file) => {
    dataForm.picture_disable = res.data
  }

  onMounted(() => {
    init(formRef)

    loadLanguageList()
  })
</script>
